//
//  MDDOrderDetailViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/5/26.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MDDOrderDetailViewController.h"
#import "MDDOrderModel.h"
#import "OrderDetailInfoCell.h"
#import "OrderDetailInfoAddressCell.h"
#import "OrderDetailStoreInfo.h"
#import "OrderDetailGoodsInfoCell.h"
#import "OrderOperateCell.h"
#import "UIView+LLXAlertPop.h"
#import "StoreMapShowViewController.h"

@interface MDDOrderDetailViewController ()
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic,strong) MDDOrderModel *orderModel;
@property (nonatomic,strong) NSDictionary *storeInfoDic;
@end

@implementation MDDOrderDetailViewController
-(void)contentAction:(UIButton *)sender
{
    NSArray *arrayTitle = [self.storeInfoDic[@"telphone"] componentsSeparatedByString:@","];
    UIColor *color = [UIColor AxcUI_colorWithHexCode:@"#676767"];
    [self.view createAlertViewTitleArray:arrayTitle textColor:color font:[UIFont systemFontOfSize:16] actionBlock:^(UIButton * _Nullable button, NSInteger didRow) {
        //获取点击事件
        NSLog(@"%@,%ld",button.currentTitle,(long)didRow);
        //                lab.text = [NSString stringWithFormat:@"%@,下标%ld",button.currentTitle,(long)didRow];
        NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", button.currentTitle];
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    mNavitationTitle(@"订单详情");
    [self addRightBarButtonItemWithTitle:@"联系商家" action:@selector(contentAction:)];
    self.view.backgroundColor = [UIColor AxcUI_colorWithHexCode:@"#ebebeb"];
    [self customNavigationItems];
    [self loadOrderDtailInfo];
    
}
-(void)backAction
{
    if (self.isCart) {
        NSInteger index = (NSInteger)[[self.navigationController viewControllers] indexOfObject:self];
        if (index > 2) {
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:(index-2)] animated:YES];
        }else{
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)loadOrderDtailInfo
{
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, self.orderNo, @"orderNo");
    setDickeyobj(parameters, @"1", @"add");
    [PPNetworkHelper POST:app_order_detail parameters:parameters success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            self.orderModel = [[MDDOrderModel alloc]init];
            [self.orderModel setValuesForKeysWithDictionary:responseObject[@"data"][@"newTOrderVO"]];
            self.storeInfoDic = responseObject[@"data"][@"newTStoreAttVO"];
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        
    } showLoading:self.view];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return 2;
    }
    return 1;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        OrderDetailInfoCell * infoCell = mGetTableViewCell(@"orderDetailInfoCell");
        infoCell.orderNumLabel.text = [NSString stringWithFormat:@"订单号:%@",self.orderNo];
        infoCell.createTimeLabel.text = [NSString stringWithFormat:@"下单时间:%@",self.orderModel.createTime];
        infoCell.tradeWaysLabel.text = [NSString stringWithFormat:@"交易方式:%@",@"货到付款"];
        if (self.orderModel.orderStatus.integerValue == 0) {
            infoCell.stateLabel.text = @"待发货";
        }else if (self.orderModel.orderStatus.integerValue == 10){
            infoCell.stateLabel.text = @"已发货";
        }else if(self.orderModel.orderStatus.integerValue == 20  ){
            infoCell.stateLabel.text = @"已送达";
        }
        return infoCell;
    }else if (indexPath.section == 1){
        OrderDetailInfoAddressCell * addInfoCell = mGetTableViewCell(@"orderDetailInfoAddressCell");
        addInfoCell.phoneLabel.text = self.orderModel.buyerPhone;
        addInfoCell.nameLabel.text = [NSString stringWithFormat:@"收件人:%@",self.orderModel.buyerName];
        addInfoCell.addressLabel.text = [NSString stringWithFormat:@"收货地址:%@",self.orderModel.buyerAddress];
        return addInfoCell;
    }else if(indexPath.section == 2){
        if (indexPath.row == 0) {
            OrderDetailStoreInfo * addInfoCell = mGetTableViewCell(@"orderDetailStoreInfo");
            return addInfoCell;
        }else{
            OrderDetailGoodsInfoCell * odiac = mGetTableViewCell(@"orderDetailGoodsInfoCell");
            odiac.orderModel = self.orderModel;
            return odiac;
        }
    }else if (indexPath.section == 3){//最下面的cell， 没有传值的
        if (indexPath.row == 0) {
            OrderOperateCell * orderOperateCell = mGetTableViewCell(@"orderOperateCell");
            orderOperateCell.youHuiLabel.text = [NSString stringWithFormat:@"%@元",self.orderModel.orderPlatformDiscount];//平台优惠
            orderOperateCell.yunFeiLabel.text = [NSString stringWithFormat:@"运费总计：%@元",self.orderModel.orderPostage];
            orderOperateCell.priceTotalLabel.text = [NSString stringWithFormat:@"订单总金额：%@元",self.orderModel.orderPrice];
            orderOperateCell.addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",self.storeInfoDic[@"province"],self.storeInfoDic[@"city"],self.storeInfoDic[@"county"],self.storeInfoDic[@"detailAddress"]];
            orderOperateCell.beiZhuLabel.text = [NSString stringWithFormat:@"备注:%@",self.orderModel.remark];
            WeakSelf(weakSelf);
            orderOperateCell.daoHangBlock = ^{
                 StoreMapShowViewController * smsvc = [[StoreMapShowViewController alloc]init];
                StoreInfoItem * storeModel = [[StoreInfoItem alloc]init];
                storeModel.storeName = self.storeInfoDic[@"storeName"];
                storeModel.longitude = self.storeInfoDic[@"longitude"];
                storeModel.latitude =  self.storeInfoDic[@"latitude"];
                smsvc.storeItem = storeModel;
                [weakSelf.navigationController pushViewController:smsvc animated:YES];
            };
            return orderOperateCell;
        }
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 160;
    }else if (indexPath.section == 1){

        return 145;
    }else if(indexPath.section == 2){
        if (indexPath.row == 0) {
            return 44;
        }
        return [OrderDetailGoodsInfoCell cellHeigtForModel:self.orderModel ];
    }else if (indexPath.section == 3){
        if (indexPath.row == 0) {
            return 280;
        }
    }
    
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
